#include "my_lib/bluetooth.h"

BluePack_t bluePack = {0};
static uint8_t receiveLen = 0, startFlag =0, receiveFlag =0;
static uint8_t tempBuffer[TOTAL_LENTH];


void BlueInit(void)
{
    //外设时钟使能
    RCC_AHBPeriphClk_Enable(RCC_AHB_PERIPH_GPIOB, ENABLE);
    RCC_APBPeriphClk_Enable1(RCC_APB1_PERIPH_UART2, ENABLE);


    GPIO_InitTypeDef GPIO_InitStructure;

    //UART TX RX 复用
    PB01_AFx_UART2TXD();
    PB00_AFx_UART2RXD();

    GPIO_InitStructure.Pins = GPIO_PIN_1;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_Init(CW_GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = GPIO_PIN_0;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
    GPIO_Init(CW_GPIOB, &GPIO_InitStructure);

    //uart config
    USART_InitTypeDef USART_InitStructure;
    USART_InitStructure.USART_BaudRate = 9600;   //botel
    USART_InitStructure.USART_Over = USART_Over_16;
    USART_InitStructure.USART_Source = USART_Source_PCLK;
    USART_InitStructure.USART_UclkFreq = 64000000;   //时钟pinl
    USART_InitStructure.USART_StartBit = USART_StartBit_FE;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No ;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(CW_UART2, &USART_InitStructure);

    //优先级，无优先级分组
    NVIC_SetPriority(UART2_IRQn, 0);
    //UARTx中断使能
    NVIC_EnableIRQ(UART2_IRQn);
    USART_ITConfig(CW_UART2, USART_IT_RC, ENABLE);

    //temp test
    USART_SendData_8bit(CW_UART2, 'H');
}



void BlueGetData(uint8_t info)
{
    if(info == PACK_HEAD){
        receiveLen = 0;
        startFlag = 1;
    }
    else if(info == PACK_TAIL){
        startFlag =0;
        receiveFlag = 1;
    }
    else if(startFlag)
    {
        tempBuffer[receiveLen++] = info;
    }
}


void BlueHandler(uint16_t cycle)
{
    static uint32_t timeTick=0;
    if((GetTick()-timeTick) > cycle)
    {
        timeTick = GetTick();

        if(receiveFlag)
        {
            receiveFlag = 0;
            
            uint8_t* pBool = (uint8_t*)tempBuffer;
            for(uint8_t i=0; i<BOOL_NUM; i++)
            {
                bluePack.mBool[i] = *pBool;
                pBool++;
            }

            int8_t* pByte = (int8_t*)pBool;
            for(uint8_t i=0; i<BYTE_NUM; i++)
            {
                bluePack.mInt8[i] = *pByte;
                pByte++;
            }

            // int16_t* pShort = (int16_t*)pByte;
            // for(uint8_t i=0; i<SHORT_NUM; i++)
            // {
            //     bluePack.mInt16[i] = *pShort;
            //     pShort++;
            // }

            // int32_t* pInt = (int32_t*)pShort;
            // for(uint8_t i=0; i<INT_NUM; i++)
            // {
            //     bluePack.mInt32[i] = *pInt;
            //     pInt++;
            // }

            // float* pFloat = (float*)pInt;
            // for(uint8_t i=0; i<FLOAT_NUM; i++)
            // {
            //     bluePack.mFloat[i] = *pFloat;
            //     pFloat++;
            // }

            printf("bool:%d, byte1:%d, short2:%d, short3:%d, short4:%d\r\n", bluePack.mBool[0], bluePack.mInt8[0], bluePack.mInt8[1],bluePack.mInt8[2], bluePack.mInt8[3]);
        }
    }
}



